<?php

class Application_Model_AiRobots {

	public function getSentence($sentence){
		$symbols = array(",",".","?","!","~","@","#","$","%","^","&","*","(",")"."_","-","+","=","<",">","/",":",";",'"',"[","]","{","}","|");
		$words = explode(" ",$sentence);
		$result = null;
		foreach($words as $word){
			$pos = 0;
			$flag = 0;
			while($flag == 0){
				$lastWord = substr($word,strlen($word)-$pos-1,1);
				$check = 0;
				foreach($symbols as $symbol){
					if($symbol == $lastWord){
						$pos ++;
						$check = 1;
					}
				}
				if($check == 0){
					if(substr($word,strlen($word)-2)=="'s"){
						$result[] = substr($word,0,strlen($word)-2);
						$result[] = "is";
					}else if(substr($word,strlen($word)-2)=="'m"){
						$result[] = substr($word,0,strlen($word)-2);
						$result[] = "am";
					}
					else{
						$result[] = substr($word,0,strlen($word)-$pos);
					}
					$flag = 1;
				}
			}
		}
		return $result;
	}
	
	public function teach($ask,$answer,$id){
		//for ai teaching
		$insertask = str_replace("'","",$ask);
		$answer = str_replace("'","",$answer);
		$msgDb = new Application_Model_DbTable_Messages();
		$responseDb = new Application_Model_DbTable_Response();
		$row1 = $msgDb->getMessage($insertask,$id);
		if($row1 == null){
			$row2 = $msgDb->createMessage($insertask,$id);
			$responseDb -> createResponse($answer,$row2->id);
		}else{
			$responseDb -> createResponse($answer,$row1->id);
		}
		
		//for user teaching
		$wordsDb = new Application_Model_DbTable_Words();
		$pairsDb = new Application_Model_DbTable_Pairs();
		
		$ws =  $this->getSentence($ask);
		$wordsId = null;
		//add reply
		$reply = $this->addReply($answer,$id);
		//add word
		foreach($ws as $w){
			$r = $wordsDb->getWord($w,$id);
			if($r == null){
				$newWord = $wordsDb->createWord($w,$id);
				$wordsId[] = $newWord->id;
			}else{
				$wordsId[] = $r->id;
			}
		}
		//add reply
		foreach($wordsId as $wId){
			$rp = $pairsDb->getPair($wId,$reply->id);
			if($rp == null){
				$pairsDb->createPair($wId,$reply->id,20);
			}else{
				$pairsDb->updatePair($wId,$reply->id,2);
			}
		}
		
		return "s";
	}

	public function teach2($ask,$answer,$id){
		//for ai teaching
		$insertask = str_replace("'","",$ask);
		$answer = str_replace("'","",$answer);
		$msgDb = new Application_Model_DbTable_Messages();
		$responseDb = new Application_Model_DbTable_Response();
		$row1 = $msgDb->getMessage($insertask,$id);
		if($row1 == null){
			$row2 = $msgDb->createMessage($insertask,$id);
			$responseDb -> createResponse($answer,$row2->id);
		}else{
			$responseDb -> createResponse($answer,$row1->id);
		}
		
		//for user teaching
		$wordsDb = new Application_Model_DbTable_Words();
		$pairsDb = new Application_Model_DbTable_Pairs();
		
		$ws =  $this->getSentence($ask);
		$wordsId = null;
		//add reply
		$reply = $this->addReply($answer,$id);
		//add word
		foreach($ws as $w){
			$r = $wordsDb->getWord($w,$id);
			if($r == null){
				$newWord = $wordsDb->createWord($w,$id);
				$wordsId[] = $newWord->id;
			}else{
				$wordsId[] = $r->id;
			}
		}
		//add reply
		foreach($wordsId as $wId){
			$rp = $pairsDb->getPair($wId,$reply->id);
			if($rp == null){
				$pairsDb->createPair($wId,$reply->id,5);
			}else{
				$pairsDb->updatePair($wId,$reply->id,1);
			}
		}
		
		return "s";
	}	
	
	public function learning($ask,$answer,$id){
		$wordsDb = new Application_Model_DbTable_Words();
		$pairsDb = new Application_Model_DbTable_Pairs();
		$ws =  $this->getSentence($ask);
		$wordsId = null;
		$reply = $this->addReply($answer,$id);
		foreach($ws as $w){
			$r = $wordsDb->getWord($w,$id);
			if($r == null){
				$newWord = $wordsDb->createWord($w,$id);
				$wordsId[] = $newWord->id;
			}else{
				$wordsId[] = $r->id;
			}
		}
		foreach($wordsId as $wId){
			$rp = $pairsDb->getPair($wId,$reply->id);
			if($rp == null){
				$pairsDb->createPair($wId,$reply->id,1);
			}else{
				$pairsDb->updatePair($wId,$reply->id,1);
			}
		}
	}
	
	public function chat($ask,$id){
		$wordsDb = new Application_Model_DbTable_Words();
		$pairsDb = new Application_Model_DbTable_Pairs();
		$replyDb = new Application_Model_DbTable_Reply();
		
		$replies = $replyDb->getAllReplies($id);
		$n = count($replies);
		if($n == 0)
			return "teach me, please";		
		$pReplies = null;
		$total = 0;
		foreach($replies as $r){
			$total = $total + $r->ct;
		}
		foreach($replies as $r){
			$pReplies[$r->id]['reply'] = $r->reply;
			$pReplies[$r->id]['p'] = ($r->ct)/$total;
		}
		$words = $this->getSentence($ask);
		$wcs = null;
		$wps = null;
		foreach($replies as $r){
			$rows = $pairsDb->getWord($r->id);
			$total2 = 0;
			foreach($rows as $row){
				$total2 = $total2 + $row->ct;
			}
			$wcs[$r->id] = $total2;
		}
		foreach($replies as $r){
			foreach($words as $word){
				$row = $wordsDb->getWord($word,$id);
				if($row == null){
					$wps[$r->id][] = 1/$wcs[$r->id];
				}else{
					$pair = $pairsDb->getPair($row->id,$r->id);
					if($pair == null){
						$wps[$r->id][] = 1/$wcs[$r->id];
					}else{
						$wps[$r->id][] = $pair->ct/$wcs[$r->id];
					}
				}
			}
		}
		$prTotal = 0;
		$prs = null;
		foreach($replies as $r){
			$n = 1;
			foreach($wps[$r->id] as $p){
				$n = $n*$p;
			}
			$n = $n * $pReplies[$r->id]['p'];
			$prTotal = $prTotal + $n;
		}

		foreach($replies as $r){
			$n = 1;
			foreach($wps[$r->id] as $p){
				$n = $n*$p;
			}
			$n = $n * $pReplies[$r->id]['p'];
			$prs[$r->id]['p'] = $n / $prTotal;
			$prs[$r->id]['reply'] = $pReplies[$r->id]['reply'];
		}
		
		$randNum = rand(0,1000000)/1000000;	
		$temp = 0;
		$checkDK = 0;
		//print_r($prs);
		foreach($prs as $pr){
			if(($pr['p']+$temp)>$randNum){
				return $pr['reply'];
				break;
			}else{
				$temp += $pr['p'];
			}
		}
	}
	
	public function addReply($reply,$id){
		$replyDb = new Application_Model_DbTable_Reply();
		$row = $replyDb->getReply($id,$reply);
		if($row == null){
			$result = $replyDb->createReply($id,$reply,1);
			return $result;
		}else{
			$result = $replyDb->updateReply($id,$reply,1);
			return $result;
		}
	}
}

